package superbubble.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import superbubble.entry.Graph;
import superbubble.entry.Edge;

public class ReadFromFile {
	private static  int numberNodes;

	 public static byte [][] readMatrixFromFile(String filename) {
		  byte[][] result = null;
		  try {
		   File inputFile = new File(filename);
		   BufferedReader in = new BufferedReader(new FileReader(inputFile));

		   String inputLine = in.readLine();
		   String[] settingsData = inputLine.split(" ");
		   int linkNumber = Integer.valueOf(settingsData[0]);
		   String recordsNumber = settingsData[1];
		   result = new byte[linkNumber+1][linkNumber+1];
		   numberNodes = linkNumber;
		   while ((inputLine = in.readLine()) != null) {
		    String[] lineData = inputLine.split(" ");
		    int startLink = Integer.valueOf(lineData[0]);
		    int endLink = Integer.valueOf(lineData[1]);
//		    String weight = lineData[2];
		    result[startLink][endLink]=1;
		   }
		   in.close();
		  } catch (IOException e) {
		   e.printStackTrace();
		  }
		  return result;
		 }
	public static Graph readGrahpFromFile(String filename) {
		List<Edge> result = null;
		Graph graph = new Graph();
		try {
			File inputFile = new File(filename);
			BufferedReader in = new BufferedReader(new FileReader(inputFile));

			String inputLine = in.readLine();
			String[] settingsData = inputLine.split(" ");
			String linkNumber = settingsData[0];
			String recordsNumber = settingsData[1];
			result = new ArrayList<Edge>(Integer.valueOf(recordsNumber));
			graph = new Graph(Integer.valueOf(linkNumber), Integer.valueOf(recordsNumber));
			while ((inputLine = in.readLine()) != null) {
				String[] lineData = inputLine.split(" ");
				String startLink = lineData[0];
				String endLink = lineData[1];
				String weight = lineData[2];
				Edge graphLink = new Edge(Integer.valueOf(startLink),
						Integer.valueOf(endLink), Integer.valueOf(weight));
				result.add(graphLink);
				graph.addNode(Integer.valueOf(startLink));
				graph.addNode(Integer.valueOf(endLink));
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		graph.setGraphLinks(result); 
		return graph;
	}

	public static int getNumberNodes() {
		// TODO Auto-generated method stub
		return numberNodes;
	}
	
}
